using System;
using System.Collections.Generic;
using System.Text;
using Ent.SqlDB;
using System.Data;

namespace Ent.Voice
{
    public enum MessagePostResponse { Posted, Error }
    public class Message
    {
        public int Id;
        public string ToUserId = "";
        public string FromUserId = "";
        public string Subject = "";
        public string Content = "";
        public DateTime PostDate = DateTime.Now;
        public string InReference = "";
        public bool IsRead = false;
        public string FromUserName = "";
        public static MessagePostResponse Post(Message msg)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("ToUserId", msg.ToUserId);
            pars.Add("FromUserId", msg.FromUserId);
            pars.Add("Subject", msg.Subject);
            pars.Add("Content", msg.Content);
            pars.Add("InReference", msg.InReference);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_PostUserMessages", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            switch (res.ToLower())
            {
                case "posted":
                    return MessagePostResponse.Posted;
                default:
                    return MessagePostResponse.Error;
            }
        }
        public static Message[] GetUserMessages(string UserId, int startIndex, int count, ref int total)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("ToUserId", UserId);
            pars.Add("startIndex", startIndex);
            pars.Add("rowCount", count);
            pars.Add("orderBy", "Postdate");
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("count", new VSqlDbType(SqlDbType.Int, -1));
            ReaderResponse rr = Execute.SPReader("Voice_GetUserMessages", pars, outpars);
            total = Convert.ToInt32(rr.OutParams["count"]);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Message[] retValue = new Message[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    retValue.SetValue(SetMessage(rr.Table.Rows[i]), i);
                }
                return retValue;
            }
            else
                return null;
        }
        public static Message GetUserMessage(int messageId, string UserId)
        {
            return GetUserMessage(messageId, UserId, true);
        }
        public static Message GetUserMessage(int messageId, string UserId, bool setRead)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("ToUserId", UserId);
            pars.Add("MessageId", messageId);
            pars.Add("SetRead", (setRead ? 1 : 0));
            ReaderResponse rr = Execute.SPReader("Voice_GetUserMessage", pars, null);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
                return SetMessage(rr.Table.Rows[0]);
            else
                return null;
        }
        public static void DeleteMessage(int messageId, string UserId)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("UserId", UserId);
            pars.Add("MessageId", messageId);
            Execute.SPNonQuery("Voice_DeleteMessage", pars, null);
        }
        private static Message SetMessage(DataRow dr)
        {
            Message retValue = new Message();
            if (dr.Table.Columns.Contains("Id")) retValue.Id = Convert.ToInt32(dr["Id"]);
            if (dr.Table.Columns.Contains("ToUserId")) retValue.ToUserId = dr["ToUserId"].ToString();
            if (dr.Table.Columns.Contains("FromUserId")) retValue.FromUserId = dr["FromUserId"].ToString();
            if (dr.Table.Columns.Contains("Subject")) retValue.Subject = dr["Subject"].ToString();
            if (dr.Table.Columns.Contains("Content")) retValue.Content = dr["Content"].ToString();
            if (dr.Table.Columns.Contains("PostDate")) retValue.PostDate = Convert.ToDateTime(dr["PostDate"]);
            if (dr.Table.Columns.Contains("InReference")) retValue.InReference = dr["InReference"].ToString();
            if (dr.Table.Columns.Contains("IsRead")) retValue.IsRead = Convert.ToBoolean(dr["IsRead"]);
            if (dr.Table.Columns.Contains("FromUserName")) retValue.FromUserName = dr["FromUserName"].ToString();
            return retValue;
        }
    }
}
